-
Notifications
You must be signed in to change notification settings - Fork 470
feat(config): add customGetElementError config option #452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(config): add customGetElementError config option #452
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 31040e5:
|
Codecov Report
@@ Coverage Diff @@
## master #452 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 22 22
Lines 385 385
Branches 91 91
=========================================
Hits 385 385 Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use the queries object so the list of queries doesn't need to be maintained. the other comment is a total nit. :)
a8362cb
to
4b2228e
Compare
(query, showLogOnFail) => { | ||
configure({showLogOnFail}) | ||
document.body.innerHTML = '<div>Hello</div>' | ||
expect(() => screen[query]('TEST QUERY')).toThrowErrorMatchingSnapshot() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer tests that specifically look for the presence/absence of the error message (not a fan of snapshot tests personally) but I'm not gonna block for it. I'll defer to @kentcdodds or someone else on that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like this?
const div = '<div>Hello</div>'
document.body.innerHTML = div
try {
screen[query]('TEST QUERY'))
} catch (err) {
expect(err.message.includes(div)).toBe(showLogOnFail)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine to me. I'll defer to @kentcdodds though on the snapshot tests
Yeah that would work
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really find myself using this option. In CI you want it anyway and during dev most of the time as well. I hold off voting for this one.
@eps1lon this is specifically useful in e2e tests where logging the entire Dom is never going to be useful. Even in CI I'd say you don't want it as the Dom will usually be higher than the limit anyway. In RTL tests sure but for testcafe, Cypress etc full Dom logging Is just noise. |
@kentcdodds any objections to merging this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fine to me. Just one thing, then anyone can feel free to merge.
4b2228e
to
9cc7ce9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super. Thank you!
👍 |
Hold it. Hmm.... I just had an idea. What if we invert control and instead hand the logging responsibility to you if you want it. |
So something like: config.customGetElementError((message, container) => {
// do whatever. So to support what you want here it'd be:
return new Error(message)
}) I think I'd be happier with that :) |
@kentcdodds, Ok. I'll try implementing that |
9cc7ce9
to
997bda7
Compare
997bda7
to
1be0482
Compare
src/query-helpers.js
Outdated
const errorMessages = [message, prettyDOM(container)] | ||
if (customGetElementError) { | ||
return customGetElementError(...errorMessages) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd prefer it if it were this way:
const errorMessages = [message, prettyDOM(container)] | |
if (customGetElementError) { | |
return customGetElementError(...errorMessages) | |
} | |
if (customGetElementError) { | |
return customGetElementError(message, container) | |
} | |
const errorMessages = [message, prettyDOM(container)] |
That way the developer writing their custom error getter can pretty-ify the container if they want, but they can do something else with it if they'd rather.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that too, but I wasnt sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kentcdodds just updated with that last suggestion and rebased.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for that!
One other thing, with this now I think we can make things even better. It's hard to explain so I hope you don't mind if I make the change myself and you can ask me questions/provide feedback if you have any.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that refactor makes sense
1be0482
to
8b8307a
Compare
🎉 This PR is included in version 6.13.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
@all-contributors please add @aw-davidson for code and tests |
I've put up a pull request to add @aw-davidson! 🎉 |
Closes #360
What:
Adding a showLogOnFail option to configure logging for getBy* and getAllBy* when the query fails.
Why:
Logging the entire document when working with larger components can obscure other error messages. I often find these explicit error messages are more helpful than scrolling through the output from prettyDom. If the DOM needs to be examined then debug() can be used. @benmonro also mentions that this makes working with Testcafe harder.
How:
getElementError
looks at the config and determines what to output as an error:Checklist:
docs site
DefinitelyTyped